package com.xuliugen.security.pc.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

/**
 * 自定义用户认证逻辑：用户信息获取，使用UserDetailsService的实现类
 * Created by xuliugen on 2017/11/5.
 */
@Component
public class UserDetailServiceImpl implements UserDetailsService, SocialUserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private final PasswordEncoder passwordEncoder;

    @Autowired
    public UserDetailServiceImpl(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    /**
     * 根据用户名查找用户信息
     * @param username 登录用户名
     * @return UserDetails接口的实现类
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登录用户名：{}", username);

        //TODO 使用username去数据库等存储查找用户信息封装为UserDetails实现类返回

        //根据查找到的用户信息判断用户是否被冻结等信息
        //* @param username the username presented to the DaoAuthenticationProvider
        //* @param password the password that should be presented to the DaoAuthenticationProvider
        //* @param enabled set to true if the user is enabled
        //* @param accountNonExpired set to true if the account has not expired
        //* @param credentialsNonExpired set to true if the credentials have not expired
        //* @param accountNonLocked set to true if the account is not locked
        //* @param authorities the authorities that should be granted to the caller if they presented
        //         the correct username and password and the user is enabled. Not null.
        return new User(username, passwordEncoder.encode("123456"),
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        logger.info("设计登录用户Id:" + userId);
        return buildUser(userId);
    }

    private SocialUserDetails buildUser(String userId) {
        // 根据用户名查找用户信息
        //根据查找到的用户信息判断用户是否被冻结
        String password = passwordEncoder.encode("123456");
        logger.info("数据库密码是:" + password);
        return new SocialUser(userId, password,
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
